package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * rabbitmq 监听
 *
 * @author zwj
 * @version 1.0
 * @createTime 2024/9/7
 */
@Slf4j
@Component
public class SpringRabbitListener {
    /**
     * 监听器用于处理名为 "simple.queue" 的队列中的消息
     * 当接收到队列中的消息时，该方法会打印接收到的消息，并抛出一个自定义的异常
     *
     * @param msg 从队列中接收到的消息
     */
    @RabbitListener(queues = "simple.queue")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "simple.queue"),
            exchange = @Exchange(name = "hmall.simple", type = ExchangeTypes.FANOUT)
    ))
    public void listenSimpleQueueMessage(String msg) {
        System.out.println("spring消费者接受到消息：【" + msg + "】,dateTime:{}" + LocalDateTime.now());
        throw new MessageConversionException("消息处理失败");
//        throw new RuntimeException("消息处理失败");
    }

    /**
     * 监听并处理名称为“work.queue”队列中的消息。
     * 该方法会打印当前时间和接收到的消息内容，并模拟处理消息的过程。
     *
     * @param msg 从队列中接收到的消息字符串。
     * @throws InterruptedException 如果处理消息过程中发生中断异常。
     */
    @RabbitListener(queues = "work.queue")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "work.queue"),
            exchange = @Exchange(name = "hmall.work", type = ExchangeTypes.FANOUT)
    ))
    public void listenWorkQueueMessage(String msg) throws InterruptedException {
        System.out.println("消费者一：spring消费者接受到消息：【" + msg + "】" + LocalDateTime.now());
        //模拟处理消息时长
        Thread.sleep(20);
    }

    /**
     * 监听并处理名称为“work.queue”队列中的消息。
     * 该方法会打印当前时间和接收到的消息内容，并模拟处理消息的过程。
     *
     * @param msg 从队列中接收到的消息字符串。
     * @throws InterruptedException 如果处理消息过程中发生中断异常。
     */
    @RabbitListener(queues = "work.queue")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "work.queue"),
            exchange = @Exchange(name = "hmall.work", type = ExchangeTypes.FANOUT)
    ))
    public void listenWorkQueueMessage2(String msg) throws InterruptedException {
        System.err.println("消费者二：spring消费者接受到消息：【" + msg + "】" + LocalDateTime.now());
        //模拟处理消息时长
        Thread.sleep(200);
    }

    /**
     * 监听并处理名称为“fanout.queue1”和“fanout.queue2”队列中的消息。
     * 该方法会打印当前时间和接收到的消息内容。
     *
     * @param msg 从队列中接收到的消息字符串。
     * @throws InterruptedException 如果处理消息过程中发生中断异常。
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue1"),
            exchange = @Exchange(name = "hmall.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueueMessage1(String msg) throws InterruptedException {
        System.err.println("消费者一：spring消费者接受到消息：【" + msg + "】" + LocalDateTime.now());
    }

    /**
     * 监听并处理名称为“fanout.queue2”队列中的消息。
     * 该方法会打印当前时间和接收到的消息内容。
     *
     * @param msg 从队列中接收到的消息字符串。
     * @throws InterruptedException 如果处理消息过程中发生中断异常。
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue2"),
            exchange = @Exchange(name = "hmall.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueueMessage2(String msg) throws InterruptedException {
        System.err.println("消费者二：spring消费者接受到消息：【" + msg + "】" + LocalDateTime.now());
    }

    /**
     * 基于注解声明交换机与队列
     *
     * @param msg 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }

    /**
     * 基于注解声明交换机与队列
     *
     * @param msg 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }

    /**
     * 基于注解声明交换机与队列
     *
     * @param msg 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"))
    public void listenTopicQueue1(String msg) {
        System.out.println("topic.queue1:" + msg);
    }

    /**
     * 基于注解声明交换机与队列
     *
     * @param msg 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"))
    public void listenTopicQueue2(String msg) {
        System.out.println("topic.queue2:" + msg);
    }

    /**
     * 监听并处理名为“object.queue”队列中的消息。
     * 该方法会打印接收到的消息内容。
     *
     * @param msg 从队列中接收到的消息字符串。
     */
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(HashMap<String, Object> msg) {
        System.out.println("消费者object.queue接收到的消息是：" + msg.toString());
    }

    /**
     * 定义死信交换机
     *
     * @param msg 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "dlx.direct"),
            key = {"hi"}
    ))
    public void listenDlxQueue(String msg) {
        log.info("消费者监听到  dlx.queue 的消息：【{}】", msg);
    }


    /**
     * 监听延迟队列中的消息
     * 此方法被配置为监听名为 "delay.queue" 的队列中的消息。
     * 当有消息到达时，它将被自动调用，并将消息内容打印到日志中。
     *
     * @param msg 从延迟队列中接收到的消息字符串。
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.exchange", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayMessage(String msg) {
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }
}
